package com.andnnl.groupby;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by chenss on 2021/1/29.
 */
public class ListMapGroupByTest {

    private static List<Map<String, Integer>> convertMap(List<Map> list) {
        List<Map<String, Integer>> ll = new ArrayList<>();
//        list.stream().flatMap(e->e.entrySet().stream()).forEach(e->{
//            System.out.println(e);
//        });
        for (Map map : list) {
            Map<String, Integer> rsMap = new HashMap<>();
            map.forEach((k, v) -> {
                rsMap.put(k.toString(), Integer.parseInt(v.toString()));
            });
            ll.add(rsMap);
        }

//        List<Map<String, Integer>> ll2=
        Map<String, Integer> xx = (Map<String, Integer>) list.stream().flatMap(e -> e.entrySet().stream())
                .collect(Collectors.toMap((Map.Entry k) -> k.getKey(), (Map.Entry k) -> Integer.parseInt(k.getValue().toString())
                        , (v1, v2) -> Integer.parseInt(v1.toString()) + Integer.parseInt(v2.toString())
                ));
        System.out.println("xx:" + xx);
        return ll;
    }

    public static void main(String[] args) {

        //先用groupingBy分组，再使用joining将某个字段进行拼接
        List<Bar> list=new ArrayList<>();
        Map<Integer, String> map = list.stream().collect(
                Collectors.groupingBy(Bar::getCount,
                        Collectors.mapping(Bar::getName, Collectors.joining(","))));

        test1();
        test2();
//        list.stream() //
//                .flatMap((map) -> map.entrySet().stream())
//                .forEach((Map.Entry e)-> {
//                    System.out.println(""+e);
//                });

//                .collect(Collectors.toMap(e->e.getKey(), e-> Integer.parseInt(e.getValue().toString()), Integer::sum));

//        Map map=new HashMap<>();
//        map.put("test1",1);
//        map.put("test2",1);
//        List<Map> list2=new ArrayList<>();
//        list2.add(map);
//        Map<String, Integer> sumByName =list.stream().flatMap((m) -> m.entrySet().stream())
//                .collect(Collectors.groupingBy(Map.Entry::getKey,Collectors.summingInt(Map.Entry::getKey)));
//        Map<String, Long> sumByName = list2.stream().flatMap((m) -> m.entrySet().stream()).collect(
//                Collectors.groupingBy(t -> Map.Entry.getKey(t), Collectors.summingLong(Map.Entry::getKey))
//        );
//        System.out.println(sumByName);


    }

    private static void test1() {
        List<Map<String, Integer>> list = new ArrayList<>();
//        List<Map> list = new ArrayList<>();

        Random random = new Random();
        String[] strings = {"A", "B", "C"};
        for (int i = 0; i < 5; i++) {
            Map<String, Integer> map = new HashMap<>();
//            Map<String, Object> map = new HashMap<>();
            for (int j = 0; j < 3; j++) {
                map.put(strings[random.nextInt(3)], random.nextInt(10));
//                map.put(strings[random.nextInt(3)],"asdf"+i);
            }
            list.add(map);
        }
        System.out.println(list);
//        List<Map<String, Integer>> list2 = convertMap(list);
//        System.out.println(list2);
        Map<String, Integer> sum = list.stream() //
                .flatMap((map) -> map.entrySet().stream()) //
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Integer::sum));
        System.out.println(sum);


    }

    private static void test2() {
//        List<Map<String, Integer>> list = new ArrayList<>();
        List<Map> list = new ArrayList<>();

        Random random = new Random();
        String[] strings = {"A", "B", "C"};
        for (int i = 0; i < 5; i++) {
//            Map<String, Integer> map = new HashMap<>();
            Map<String, Object> map = new HashMap<>();
            for (int j = 0; j < 3; j++) {
                map.put(strings[random.nextInt(3)], ""+random.nextInt(10));
//                map.put(strings[random.nextInt(3)],"asdf"+i);
            }
            list.add(map);
        }
        System.out.println(list);
//        List<Map<String, Integer>> list2 = convertMap(list);
//        System.out.println(list2);
        Map<String, Integer> sum = (Map<String, Integer>) list.stream() //
                .flatMap((map) -> map.entrySet().stream()) //
                .collect(Collectors.toMap((Map.Entry e)->e.getKey(), (Map.Entry e)->Integer.parseInt(""+e.getValue()), Integer::sum));
        System.out.println(sum);
    }
}
